package behavior_analysis_control;

import java.util.Collections;
import java.util.List;

import behavior_analysis_model.Track;
import behavior_analysis_model.TrackComparator;

public class POIFinder {
	
	public static final int NUM_POIS = 5;
	
	private static final double WORK_TIME = 0.15;
	
	private static final double HOME_TIME = 0.33;
	
	private final long my_total_time;
	
	private final List<Track> my_pois;
	
	public POIFinder (List<Track> the_tracks) {
		my_pois = the_tracks;
		Collections.sort(my_pois, Collections.reverseOrder(new TrackComparator()));
		long total_time = 0;
		for (Track t : the_tracks) {
			total_time += t.getTotalTime();
		}
		my_total_time = total_time;
	}
	
	public Track getHome() throws POINotFoundException {
		Track home = my_pois.get(0);
		if (home.getTotalTime() > (double)(HOME_TIME * my_total_time)) {
			return home;
		} else {
			throw new POINotFoundException("A home location cannot be determined.");
		}
	}
	
	public Track getWork() throws POINotFoundException {
		Track work = my_pois.get(1);
		if (work.getTotalTime() > (double)(WORK_TIME * my_total_time)) {
			return work;
		} else {
			throw new POINotFoundException("A work location cannot be determined.");
		}
	}
	
	public Track getPOI (final int the_number) throws POINotFoundException  {
		int start = 0;
		try {
			getHome();
			start++;
		} catch (final POINotFoundException pnfe) {
			//do nothing
		}
		try {
			getWork();
			start++;
		} catch (final POINotFoundException pnfe) {
			//do nothing
		}
		int index = the_number + start;
		try {
			return my_pois.get(index);
		} catch (final IndexOutOfBoundsException aoobe) {
			throw new POINotFoundException("Point of interest #" + index + " cannot be determined.");
		}
	}
}
